/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.attr.ChildAttribute;
import com.inspur.edp.cef.core.data.EntityDataCollection;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.method.GetResInfoGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class ChildPropGenerator extends ClassPropertyGenerator {

  private final EntityGeneratorContext parentContext;
  protected String PrivatePropName;
  private MediatePropertyInfo privatePropertyInfo;
  private String privateChildCode;

  public ChildPropGenerator(EntityGeneratorContext parentContext, String childCode, MediatePropertyInfo propertyInfo,
      String privatePropName) {
    this.parentContext = parentContext;
    privateChildCode = childCode;
    privatePropertyInfo = propertyInfo;
    PrivatePropName = privatePropName;
  }

  protected final MediatePropertyInfo getPropertyInfo() {
    return privatePropertyInfo;
  }

  @Override
  protected TypeInfo getPropertyType() {
    return new TypeInfo(privatePropertyInfo.getReturnType().getTypeName());
  }


  @Override
  protected String getFieldName() {
    return privatePropertyInfo.getPropertyName();
  }

  @Override
  protected final String getPropertyName() {
    return privatePropertyInfo.getPropertyName();
  }

  @Override
  protected boolean isGetterOverride() {
    return true;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected final boolean hasSetMethod() {
    return false;
  }

  @Override
  protected Block buildGetMethodBody() {
    Block block = ast.newBlock();
    if (Character.isLowerCase(getPropertyInfo().getPropertyName().charAt(0))) {//小写
      ReturnStatement returnStatement = ast.newReturnStatement();
      MethodInvocation methodInvocation = ast.newMethodInvocation();
      methodInvocation.setName(ast.newSimpleName(
          Utils.getGetterMethodName(toUpperCaseFirstOne(getPropertyInfo().getPropertyName()))));
      returnStatement.setExpression(methodInvocation);
      block.statements().add(returnStatement);
    } else {
      setIfStatement(block);
      setReturnStatement(block);
    }
    return block;
  }

  private String toUpperCaseFirstOne(String s) {
      if (Character.isUpperCase(s.charAt(0))) {
          return s;
      } else {
          return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0)))
              .append(s.substring(1))
              .toString();
      }
  }

  private void setIfStatement(Block block) {
    IfStatement ifStatement = ast.newIfStatement();
    setIfExpresssion(ifStatement);
    setIfThen(ifStatement);
    block.statements().add(ifStatement);
  }

  private void setIfExpresssion(IfStatement ifStatement) {
    InfixExpression infixExpression = ast.newInfixExpression();
    infixExpression.setLeftOperand(ast.newSimpleName(PrivatePropName));
    infixExpression.setOperator(InfixExpression.Operator.EQUALS);
    infixExpression.setRightOperand(ast.newNullLiteral());

    ifStatement.setExpression(infixExpression);
  }

  private void setIfThen(IfStatement ifStatement) {
    Block block = ast.newBlock();
    Assignment assignment = ast.newAssignment();
    assignment.setOperator(Assignment.Operator.ASSIGN);
    assignment.setLeftHandSide(ast.newSimpleName(PrivatePropName));

    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(EntityDataCollection.class)
            .getType(ast));
    MediateType entityType = parentContext.getProjContext().getEntityMediateTypes().get(privateChildCode);
    MethodInvocation invocation = ExpressionUtils
        .getMethodInvocation(ast, entityType.getFullName(), GetResInfoGenerator.MethodName);
    classInstanceCreation.arguments().add(invocation);

    assignment.setRightHandSide(classInstanceCreation);

    ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
    block.statements().add(expressionStatement);
    ifStatement.setThenStatement(block);
  }

  private void setReturnStatement(Block block) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName(PrivatePropName));
    block.statements().add(returnStatement);
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    ArrayList<AnnotationInfo> list = super.getGetterAttributeList();
    if (list == null) {
      list = new ArrayList<>();
    }
      if (!(privateChildCode+"s").equals(privatePropertyInfo.getPropertyName())) {
        AnnotationInfo tempVar = new AnnotationInfo();
        tempVar.setAnnotationType(AnnotationType.Marker);
        tempVar.setTypeInfo(new TypeInfo(JsonIgnore.class));
        list.add(tempVar);
        return list;
      }

    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    annotationInfo.setTypeInfo(new TypeInfo(JsonProperty.class));
    annotationInfo.setSingleParam(ExpressionUtils.getStringLiteral(
        ast, StringUtils.toCamelCase(getPropertyName())
    ));
    list.add(annotationInfo);
    return list;
  }
}
